﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ConversionHelper.cs" company="XamlNinja">
//   2011 Richard Griffin and Ollie Riches
// </copyright>
// <summary>
//   Conversion helper - help methods for converting simple types.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace WP7Contrib.Common.Helpers
{
    using System;

    /// <summary>
    /// Conversion helper class.
    /// </summary>
    public static class ConversionHelper
    {
        /// <summary>
        /// Parses a a string value and returns the enum value, if it fails the default value function is executed.
        /// </summary>
        /// <param name="value">
        /// The string value to be converted to an enum value.
        /// </param>
        /// <param name="defaultValue">
        /// The default value function, this is executed if the enum conversion failed.
        /// </param>
        /// <typeparam name="T">The enum type.
        /// </typeparam>
        /// <returns>
        /// Returns the converted enum value or the default value.
        /// </returns>
        public static T ParseEnum<T>(string value, Func<T> defaultValue)
        {
            try
            {
                return (T)Enum.Parse(typeof(T), value, true);
            }
            catch (ArgumentException)
            {
                return defaultValue();
            }
            catch (OverflowException)
            {
                return defaultValue();
            }
        }

        /// <summary>
        /// Parses a a string value and returns the int32 value, if it fails the default value function is executed.
        /// </summary>
        /// <param name="value">
        /// The string value to be converted to an int32 value.
        /// </param>
        /// <param name="defaultValue">
        /// The default value function, this is executed if the int32 conversion failed.
        /// </param>
        /// <returns>
        /// Returns the converted int32 value or the default value.
        /// </returns>
        public static int? ToNullableInt32(string value, Func<int?> defaultValue)
        {
            try
            {
                return Convert.ToInt32(value);
            }
            catch (FormatException)
            {
                return defaultValue();
            }
            catch (OverflowException)
            {
                return defaultValue();
            }
        }

        /// <summary>
        /// Converts a string value and returns the int32 value, if it fails the 0 is returned.
        /// </summary>
        /// <param name="value">
        /// The string value to be converted to an int32 value.
        /// </param>
        /// <returns>
        /// Returns the converted int32 value or the 0.
        /// </returns>
        public static int ToInt32(string value)
        {
            try
            {
                return Convert.ToInt32(value);
            }
            catch (FormatException)
            {
                return 0;
            }
            catch (OverflowException)
            {
                return 0;
            }
        }

        /// <summary>
        /// Converts a double value and returns the int32 value, if it fails the 0 is returned.
        /// </summary>
        /// <param name="value">
        /// The string value to be converted to an int32 value.
        /// </param>
        /// <returns>
        /// Returns the converted int32 value or the 0.
        /// </returns>
        public static int ToInt32(double value)
        {
            try
            {
                return Convert.ToInt32(value);
            }
            catch (FormatException)
            {
                return 0;
            }
            catch (OverflowException)
            {
                return 0;
            }
        }

        /// <summary>
        /// Converts a string value and returns the decimal value, if it fails the 0 is returned.
        /// </summary>
        /// <param name="value">
        /// The string value to be converted to an decimal value.
        /// </param>
        /// <returns>
        /// Returns the converted decimal value or the 0.
        /// </returns>
        public static decimal ToDecimal(string value)
        {
            try
            {
                return Convert.ToDecimal(value);
            }
            catch (FormatException)
            {
                return 0;
            }
            catch (OverflowException)
            {
                return 0;
            }
        }
    }
}
